#include <iostream>
#include <unordered_map>
#include <gtest/gtest.h>
//全局事件:针对整个测试程序,提供全局事件机制,能够在测试之前配置测试环境数据,测试完毕后清理数据

//先定义环境类,通过继承 testing::Environment 的派生类来完成
class MyEnvironment : public testing::Environment
{
public:
    virtual void SetUp() override //重写的虚函数接口 SetUp 会在测试之前被调用
    {
        std::cout << "单元测试执行前的环境初始化！" << std::endl;
    }

    virtual void  TearDown() override //TearDown 会在测试完毕后调用.
    {
        std::cout << "单元测试执行后的环境清理！" << std::endl;
    }
};

TEST(MyEnvironment, test1)
{
    std::cout << "单元测试1" << std::endl;
}

TEST(MyEnvironment, test2)
{
    std::cout << "单元测试2" << std::endl;
}

std::unordered_map<std::string, std::string> mymap;
class MyMapTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        std::cout << "单元测试执行前的环境初始化！" << std::endl;
        mymap.insert({"hello", "你好"});
        mymap.insert({"bye", "再见"});
    }

    virtual void  TearDown() override
    {
        std::cout << "单元测试执行后的环境清理！" << std::endl;
    }
};

TEST(MyMapTest, test1)
{
    std::cout << "单元测试1" << std::endl;
    ASSERT_EQ(mymap.size(), 2);
    mymap.erase("hello");
}

TEST(MyMapTest, test2)
{
    std::cout << "单元测试2" << std::endl;
    ASSERT_EQ(mymap.size(), 2);
}

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new MyEnvironment);
    testing::AddGlobalTestEnvironment(new MyMapTest);
    RUN_ALL_TESTS();

    return 0;
}


// int main(int argc, char* argv[])
// {
//     testing::InitGoogleTest(&argc, argv);
//     testing::AddGlobalTestEnvironment(new MyEnvironment);
//     RUN_ALL_TESTS();

//     return 0;
// }